/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.yxw.yxnet_cd_center.common.utils;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import cn.hutool.core.util.StrUtil;

/**
 * @author Hjx
 */
public class BaseDateTime {

    public static String getShortDateString() {

        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getShortDateString(Date InDateTime) {
        if (InDateTime == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getShortDateString(long InDateTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getShortDateStringNoSlipt() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getShortDateStringNoSlipt(Date InDateTime) {
        if (InDateTime == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getShortDateStringNoSlipt(long InDateTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getLongDateTimeString() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getLongDateTimeStringNoSliptLastOneChar() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = formatter.format(currentTime);
        //String dateString = String.valueOf(System.currentTimeMillis());
        //return dateString.substring(12);
        return dateString;
    }

    public static String getLongDateTimeString(Date InDateTime) {
        if (InDateTime == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getLongDateTimeString(java.sql.Timestamp InDateTime) {
        if (InDateTime == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getLongDateTimeString(long InDateTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getLongDateStringNoSlipt() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    public static String getLongDateTimeStringNoSlipt(Date InDateTime) {
        if (InDateTime == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getLongDateTimeStringNoSlipt(java.sql.Timestamp InDateTime) {
        if (InDateTime == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getLongDateTimeStringNoSlipt(long InDateTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getYearString() {
        Date InDateTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getYearString(Date InDateTime) {
        if (InDateTime == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getMonthString() {
        Date InDateTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("MM");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getMonthString(Date InDateTime) {
        if (InDateTime == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("MM");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getDateString() {
        Date InDateTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("dd");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static String getDateString(Date InDateTime) {
        if (InDateTime == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("dd");
        String dateString = formatter.format(InDateTime);
        return dateString;
    }

    public static java.sql.Timestamp getTimestamp() {
        Date InDateTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(InDateTime);
        return java.sql.Timestamp.valueOf(dateString);
    }

    public static java.sql.Timestamp getTimestamp(Date InDateTime) {
        if (InDateTime == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(InDateTime);
        return java.sql.Timestamp.valueOf(dateString);
    }

    public static java.sql.Timestamp getTimestamp(String dateString) {
        if (dateString == null) {
            return null;
        }
        dateString = dateString.trim();
        if (dateString.equals("")) {
            return null;
        }
        try {
            return java.sql.Timestamp.valueOf(dateString);
        } catch (Exception ex) {
            return null;
        }
    }
    public static Long getDate(Date InDateTime) {
        if (InDateTime == null) {
            return null;
        }
        return InDateTime.getTime();
        //return InDateTime;
    }
    public static Date getDate() {
        Date InDateTime = new Date();
        return InDateTime;
    }

    public static Date getDate(long InDateTime) {
        Date dDay;
        try {
            dDay = new Date(InDateTime);
        } catch (Exception oe) {
            dDay = null;
        }
        return dDay;
    }

    public static Date getDate(String dateString) {
        if (dateString == null) {
            return null;
        }
        dateString = dateString.trim();
        if (dateString.equals("")) {
            return null;
        }
        Date dDay = null;
        SimpleDateFormat formatter;
        if (dateString.length() > 10) {
            dateString = dateString.substring(0,19).replaceAll("T", " ");
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else {
            formatter = new SimpleDateFormat("yyyy-MM-dd");
        }
        try {
            dDay = formatter.parse(dateString);
        } catch (Exception oe) {
            dDay = null;
        }
        return dDay;
    }
    public static Long getLongDate(String dateString) {
        if (dateString == null) {
            return null;
        }
        dateString = dateString.trim();
        if (dateString.equals("")) {
            return null;
        }
        Date dDay = null;
        SimpleDateFormat formatter;
        if (dateString.length() > 10) {
            dateString = dateString.substring(0,19).replaceAll("T", " ");
            formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        } else {
            formatter = new SimpleDateFormat("yyyy-MM-dd");
        }
        try {
            dDay = formatter.parse(dateString);
        } catch (Exception oe) {
            dDay = null;
            return null;
        }
        return dDay.getTime();
    }
    public static long TimeDifferent(Date d1, Date d2) {
        if (d1 == null) {
            return -10;
        }
        if (d2 == null) {
            return -10;
        }
        return d2.getTime() - d1.getTime();
    }

    public static long TimeDifferentNow(Date d1) {
        if (d1 == null) {
            return -10;
        }
        Date d2 = getDate();
        return d2.getTime() - d1.getTime();
    }

    public static boolean IsToday(String LongDateString) {
        String Today = getShortDateString();
        if (LongDateString.length() >= 10) {
            if (Today.equals(LongDateString.substring(0, 10))) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static Date editDay(Date dDay, int iEditDay) {
        if (null == dDay) {
            return null;
        }
        Calendar oCalendar = Calendar.getInstance();
        oCalendar.setTime(dDay);
        oCalendar.add(Calendar.DATE, iEditDay);
        return oCalendar.getTime();
    }

    public static Date editSecond(Date dDay, int iEditSecond) {
        if (null == dDay) {
            return null;
        }
        Calendar oCalendar = Calendar.getInstance();
        oCalendar.setTime(dDay);
        oCalendar.add(Calendar.SECOND, iEditSecond);
        return oCalendar.getTime();
    }

    public static long SecondDifferent(Date d1, Date d2) {
        if (d1 == null) {
            return -10;
        }
        if (d2 == null) {
            return -10;
        }
        long lTmp = d2.getTime() - d1.getTime();
        return lTmp / 1000;
    }
    public static Long getDifferentAverageForBaidu()
    {
        Long localTimeStampTmp = 0L;
        Long baiduTimeStampTmp = 0L;
        Long averageTimeStampTmp = 0L;
        localTimeStampTmp = System.currentTimeMillis();
        baiduTimeStampTmp = getRemoteTime("https://www.baidu.com");
        averageTimeStampTmp = localTimeStampTmp - baiduTimeStampTmp;
        localTimeStampTmp = System.currentTimeMillis();
        baiduTimeStampTmp = getRemoteTime("https://www.baidu.com");
        averageTimeStampTmp = averageTimeStampTmp + localTimeStampTmp -baiduTimeStampTmp;
        averageTimeStampTmp = localTimeStampTmp - baiduTimeStampTmp;
        localTimeStampTmp = System.currentTimeMillis();
        baiduTimeStampTmp = getRemoteTime("https://www.baidu.com");
        averageTimeStampTmp = averageTimeStampTmp + localTimeStampTmp -baiduTimeStampTmp;
        averageTimeStampTmp = averageTimeStampTmp/3;
        return averageTimeStampTmp;
    }
    public static Long getRemoteTime(String url)
    {
        if (StrUtil.isBlank(url)) {
            url = "https://www.baidu.com";
        }        
        Long timeStampTmp = 0L;
        StringBuffer buffer = new StringBuffer();
        // 声明SSL上下文
        javax.net.ssl.SSLContext ssl_context = null;
        try 
        {
            ssl_context = javax.net.ssl.SSLContext.getInstance("SSL");
            ssl_context.init(null, new javax.net.ssl.TrustManager[]
            {
                new javax.net.ssl.X509TrustManager() 
                {
                    @Override
                    public void checkClientTrusted (java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException 
                    {
                    }
                    @Override
                    public void checkServerTrusted (java.security.cert.X509Certificate[] chain, String authType) throws java.security.cert.CertificateException
                    {
                    }
                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() 
                    {
                        return null;
                    }
                }   
            }, null);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(ssl_context.getSocketFactory());

        // 实例化主机名验证接口
        javax.net.ssl.HostnameVerifier hostname_verifier = new javax.net.ssl.HostnameVerifier()
        {
            @Override
            public boolean verify(String hostname, javax.net.ssl.SSLSession session) 
            {
                return true;
            }
        };
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(hostname_verifier);
        javax.net.ssl.HttpsURLConnection http_url_connection = null;
        try 
        {
//              System.out.println("url"+url);
            http_url_connection = (javax.net.ssl.HttpsURLConnection)(new java.net.URL(url)).openConnection();
            http_url_connection.setDoInput(true);
            http_url_connection.setDoOutput(true);
            http_url_connection.setRequestMethod("GET");
            //http_url_connection.setRequestProperty("Content-Length", String.valueOf(parameter.length));
            http_url_connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");// "application/x-www-form-urlencoded"
            http_url_connection.setUseCaches(false);
            http_url_connection.connect();
            timeStampTmp = http_url_connection.getDate();
            // write request.
            /*
            java.io.BufferedOutputStream output_stream = new java.io.BufferedOutputStream(http_url_connection.getOutputStream());
            output_stream.write(parameter);
            output_stream.flush();
            output_stream.close();
            output_stream = null;
            */
            java.io.InputStreamReader input_stream_reader = new java.io.InputStreamReader(http_url_connection.getInputStream(), "utf-8");
            java.io.BufferedReader buffered_reader = new java.io.BufferedReader(input_stream_reader);
            buffer = new StringBuffer();
            String line;
            while ((line = buffered_reader.readLine()) != null)
            {
                buffer.append(line);
            }
            line=null;
            input_stream_reader.close();
            input_stream_reader = null;
            buffered_reader.close();
            buffered_reader = null;
            http_url_connection.disconnect();
        } 
        catch (Exception e) 
        {  
            System.out.println(e.getMessage());
            //ErrorLog.Log("httpsGet:" + e.getMessage());
            //logger.info("httpsGet:" + e.getMessage());
            e.printStackTrace();  
        }
        //System.out.println(buffer.toString());
        return timeStampTmp;
    }
    /**
     * 根据指定时间格式处理时间
     *
     * @param date
     * @param format
     * @return
     */
    public static String getStrDateByFormat(Date date, String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        String dateString = formatter.format(date);
        return dateString;
    }
}
